local blaze = require "blaze"
local game = require "club.threecard.texas"
local log = blaze.logging.get("dealer")

local M = game.ui.element()

M.store_watchers = {
    ingame = {
        ["$"] = blaze.noop,

        ["阶段变更"] = function(self, store, state, _, init)
            -- todo 动画
            if state.phase_id == game.store.ingame.state.phases.idle.id
                or state.phase_id == game.store.ingame.state.phases.ready.id then
                for i = 1, game.seat.MAX_SEAT_NO do
                    self.seats[i].poker_container:clear()
                end

                self.public_card.poker_container:clear()
            end
        end,

        ["发牌"] = function(self, store, state, action_sequence, init)
            self:deal(action_sequence)
        end,

        ["公牌"] = function(self, store, state, card_ids, init)
            self.public_card.poker_container:mopai(card_ids)
        end
    }
}

function M:ctor(ingame)
    self.res = "club.threecard.texas/ui/ingame.b:dealer"
    self.seats = ingame.seats
    self.public_card = ingame.public_card
end

function M:deal(action_sequence)
    local local_action_sequence = {}
    for i, no in ipairs(action_sequence) do
        table.insert(local_action_sequence, game.seat.to_local(no))
    end

    for card_index = 1, 2 do
        for i, local_no in ipairs(local_action_sequence) do
            local seat = self.seats[local_no]
            if seat == nil then
                log.error(string.format("没有找到seat no:%s  local_no:%s", action_sequence[i], local_no))
            end
            if local_no == 1 then
                seat.poker_container:mopai({game.store.ingame.state.self_card_ids[card_index]})
            else
                seat.poker_container:mopai({-1})
            end
        end
    end
end

return M